package com.javabasic.algorithm.leetcode;

/**
 * @author xiongmin
 * @version 0.0.1
 * @description Created by work on 2021/11/10 11:52 下午
 * @see [495. Teemo Attacking](https://leetcode-cn.com/problems/teemo-attacking/)
 */
public class TeemoAttacking {

    /**
     * 解法一：暴力解法
     * 从第一个数开始加上持续时间，判断下一个数的持续时间，看这两个时间段是否有交集，如果有减去交集时间段
     * @param timeSeries
     * @param duration
     * @return
     */
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int result = 0;
        int len = timeSeries.length, pre = -1;
        for (int i = 0; i < len; i++) {
            // 注意：去除重复的时间点，不然结果会变大
            if (pre-duration != timeSeries[i]) {
                continue;
            }
            if (pre < timeSeries[i] + duration && pre > timeSeries[i]) {
                result = result - (pre - timeSeries[i]) + duration;
            } else {
                result += duration;
            }
            pre = timeSeries[i] + duration;
        }
        return result;
    }


    /**
     * 解法二：逻辑思维题，判断前后两个数之间的距离是否大于持续时间，如果大于就加上持续时间否则就加上两个数之间的距离
     * @param timeSeries
     * @param duration
     * @return
     */
    public int findPoisonedDuration2(int[] timeSeries, int duration) {
        int result = 0;
        int len = timeSeries.length, distance;
        for (int i = 1; i < len; i++) {
            distance = timeSeries[i] - timeSeries[i - 1];
            if (distance > duration) {
                result += duration;
            } else {
                result += distance;
            }
        }
        result += duration;
        return result;
    }


}
